#include <iostream>

using namespace std;

int main()
{
    int a;
    cin >> a;
    bool CheckNumber(int i);
    int mass[9];
    int result = 0;

    for(int c = 0; c < 9; c++)
    {
        mass[c] = 0;
    }

    if(a >= 1000 && a <= 9999)
    {
        if(CheckNumber(a) == true)
        {
            cout << a;
            return 0;
        }
        else
        {
            int first2;
            first2 = a / 100;
            if(CheckNumber(first2) == true)
            {
                mass[0] = first2;
            }
            int last2;
            last2 = a % 100;
            if(CheckNumber(last2) == true)
            {
                mass[1] = last2;
            }
            int first = a  / 1000;
            int second = a % 10 % 10 % 10;
            int third = a % 10 % 10;
            int fourth = a % 10;
            if(CheckNumber(first) == true)
            {
                mass[2] = first;
            }
            if(CheckNumber(second) == true)
            {
                mass[3] = second;
            }
            if(CheckNumber(third) == true)
            {
                mass[4] = third;
            }
            if(CheckNumber(fourth) == true)
            {
                mass[5] = fourth;
            }
            int first3 = a / 10;
            if(CheckNumber(first3) == true)
            {
                mass[6] = first3;
            }
            int last3 = a % 1000;
            if(CheckNumber(last3) == true)
            {
                mass[7] = last3;
            }
            int middle = second * 10 + third;
            if(CheckNumber(middle) == true)
            {
                mass[8] = middle;
            }
            for(int f = 0; f < 9; f++)
            {
                if(mass[f] > result)
                {
                    result = mass[f];
                }
            }
        }
    }
    else
    {
        cout << "0";
        return 0;
    }
    cout << result;

    return 0;
}
bool CheckNumber(int i)
{
    for(int x = 1; x < i; x++)
    {
        if(i % x == 0 && x != 1)
        {
            return false;
        }
    }
    return true;
}
